﻿using System.Text;

using Tszy.Unlimited.Base.Extensions;
using Tszy.Unlimited.Data.AdoNet.Sql.Common;
using Tszy.Unlimited.Data.AdoNet.Sql.Components.Expressions.Where;
using Tszy.Unlimited.Data.AdoNet.Sql.Descriptors;

namespace Tszy.Unlimited.Data.AdoNet.Sql.Components.Db.Mysql
{
    /// <summary>
    /// SQL 生成器。
    /// </summary>
    public class SqlBuilder
    {
        private class BuildSqlRequest
        {
            /// <summary>
            /// 主表别名。
            /// </summary>
            public string MasterAlias { get; set; } = string.Empty;

            /// <summary>
            /// 表前缀。
            /// </summary>
            public string Prdfix { get; set; } = string.Empty;

            /// <summary>
            /// 表达式数据。
            /// </summary>
            public ExpressionContext Context { get; set; }

            /// <summary>
            /// SQL 生成器。
            /// </summary>
            public StringBuilder SqlBuilder { get; set; } = new StringBuilder();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="prdfix"></param>
        /// <param name="context"></param>
        /// <param name="showSql"></param>
        /// <param name="printSql"></param>
        /// <returns></returns>
        public static string BuildSelect(string prdfix, ExpressionContext context, bool showSql = false, Action<string> printSql = null)
        {
            var masterAlias = "master_table";

            var request = new BuildSqlRequest
            {
                MasterAlias = masterAlias,
                Prdfix = prdfix,
                Context = context,
            };

            BuildSelect(request);
            BuildSelectFrom(request);
            BuildWhere(request);
            BuildOrderBy(request);
            BuildLimit(request);

            var sql = request.SqlBuilder.ToString();

            if (showSql)
            {
                if (printSql != null)
                {
                    var log = new StringBuilder();

                    log.AppendLine("#---------------------------- SQL ----------------------------#");
                    log.AppendLine(sql);
                    log.AppendLine("#---------------------------- SQL ----------------------------#");

                    if (context.ParameterValues.Any())
                    {
                        log.AppendLine("#---------------------------- SQL 参数 ----------------------------#");
                        foreach (var key in context.ParameterValues.Keys.OrderBy(item => item))
                        {
                            log.AppendLine($"参数名：@{key}，参数值：{context.ParameterValues[key]}");
                        }
                        log.AppendLine("#---------------------------- SQL 参数 ----------------------------#");
                    }

                    printSql(log.ToString());
                }
            }

            return sql;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="prdfix"></param>
        /// <param name="context"></param>
        /// <param name="showSql"></param>
        /// <param name="printSql"></param>
        /// <returns></returns>
        public static string BuildUpdate(string prdfix, ExpressionContext context, bool showSql = false, Action<string> printSql = null)
        {
            var masterAlias = "master_table";

            var request = new BuildSqlRequest
            {
                MasterAlias = masterAlias,
                Prdfix = prdfix,
                Context = context,
            };

            BuildUpdate(request);
            BuildUpdateSetProperty(request);
            BuildWhere(request);

            var sql = request.SqlBuilder.ToString();

            if (showSql)
            {
                if (printSql != null)
                {
                    var log = new StringBuilder();

                    log.AppendLine("#---------------------------- SQL ----------------------------#");
                    log.AppendLine(sql);
                    log.AppendLine("#---------------------------- SQL ----------------------------#");

                    if (context.ParameterValues.Any())
                    {
                        log.AppendLine("#---------------------------- SQL 参数 ----------------------------#");
                        foreach (var key in context.ParameterValues.Keys.OrderBy(item => item))
                        {
                            log.AppendLine($"参数名：@{key}，参数值：{context.ParameterValues[key]}");
                        }
                        log.AppendLine("#---------------------------- SQL 参数 ----------------------------#");
                    }

                    printSql(log.ToString());
                }
            }

            return sql;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="prdfix"></param>
        /// <param name="context"></param>
        /// <param name="showSql"></param>
        /// <param name="printSql"></param>
        /// <returns></returns>
        public static string BuildDelete(string prdfix, ExpressionContext context, bool showSql = false, Action<string> printSql = null)
        {
            var masterAlias = "master_table";

            var request = new BuildSqlRequest
            {
                MasterAlias = masterAlias,
                Prdfix = prdfix,
                Context = context,
            };

            BuildDelete(request);
            BuildWhere(request);

            var sql = request.SqlBuilder.ToString();

            if (showSql)
            {
                if (printSql != null)
                {
                    var log = new StringBuilder();

                    log.AppendLine("#---------------------------- SQL ----------------------------#");
                    log.AppendLine(sql);
                    log.AppendLine("#---------------------------- SQL ----------------------------#");

                    if (context.ParameterValues.Any())
                    {
                        log.AppendLine("#---------------------------- SQL 参数 ----------------------------#");
                        foreach (var key in context.ParameterValues.Keys.OrderBy(item => item))
                        {
                            log.AppendLine($"参数名：@{key}，参数值：{context.ParameterValues[key]}");
                        }
                        log.AppendLine("#---------------------------- SQL 参数 ----------------------------#");
                    }

                    printSql(log.ToString());
                }
            }

            return sql;
        }

        #region 生成查询语句

        #region 生成 SELECT

        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        private static void BuildSelect(BuildSqlRequest request)
        {
            request.SqlBuilder.Append("SELECT");

            var fields = string.Empty;

            if (request.Context.Count)
            {
                fields = " COUNT(0)";
            }

            if (request.Context.Any)
            {
                fields = $" `{request.MasterAlias}`.`{GetTableKey(request.Prdfix, request.Context)}`";
            }

            if (request.Context.Sum != null)
            {
                fields = $" SUM({BuildSum(request.MasterAlias, request.Context)})";
            }

            if (request.Context.Average != null)
            {
                fields = $" AVG({BuildAverage(request.MasterAlias, request.Context)})";
            }

            if (request.Context.Min != null)
            {
                fields = $" MIN({BuildMin(request.MasterAlias, request.Context)})";
            }

            if (request.Context.Max != null)
            {
                fields = $" MAX({BuildMax(request.MasterAlias, request.Context)})";
            }

            if (string.IsNullOrWhiteSpace(fields))
            {
                if (request.Context.Fields.Count > 0)
                {
                    fields += " ";

                    var index = 0;
                    foreach (var field in request.Context.Fields)
                    {
                        var fieldValue = string.Empty;

                        if (!field.Alias.IsNullOrWhiteSpace() && field.Alias != field.Name)
                        {
                            fieldValue = $"`{request.MasterAlias}`.`{field.Name}` AS `{field.Alias}`";
                        }
                        else
                        {
                            fieldValue = $"`{request.MasterAlias}`.`{field.Name}`";
                        }

                        if (index == 0)
                        {
                            fields += fieldValue;
                        }
                        else
                        {
                            fields += $", {fieldValue}";
                        }

                        index++;
                    }
                }

                if (string.IsNullOrWhiteSpace(fields))
                {
                    fields = $" `{request.MasterAlias}`.*";
                }
            }

            request.SqlBuilder.Append(fields);
        }

        #region 统计

        #region 生成 SUM

        private static string BuildSum(string masterAlias, ExpressionContext context)
        {
            return BuildMathTree1(masterAlias, context.Sum);
        }

        #endregion 生成 SUM

        #region 生成 AVG

        private static string BuildAverage(string masterAlias, ExpressionContext context)
        {
            return BuildMathTree1(masterAlias, context.Average);
        }

        #endregion 生成 AVG

        #region 生成 MIN

        private static string BuildMin(string masterAlias, ExpressionContext context)
        {
            return BuildMathTree1(masterAlias, context.Min);
        }

        #endregion 生成 MIN

        #region 生成 MAX

        private static string BuildMax(string masterAlias, ExpressionContext context)
        {
            return BuildMathTree1(masterAlias, context.Max);
        }

        #endregion 生成 MAX

        #region 生成统计表达式

        private static string BuildMathTree1(string masterAlias, IWhereMathExpressionDescriptor descriptor)
        {
            if (descriptor.Value != null)
            {
                if (descriptor.Value.Tag == "+")
                {
                    return $"`{masterAlias}`.`{descriptor.Value.ParmeterName}`";
                }
                else
                {
                    return $"-`{masterAlias}`.`{descriptor.Value.ParmeterName}`";
                }
            }

            if (descriptor.ExpressionDescriptor == null)
                return string.Empty;

            return BuildMathTree2(masterAlias, descriptor.ExpressionDescriptor);
        }

        private static string BuildMathTree2(string masterAlias, WhereMathDetailExpressionDescriptor descriptor)
        {
            return $"({BuildMathTree1(masterAlias, descriptor.Left)} {descriptor.Tag} {BuildMathTree1(masterAlias, descriptor.Right)})";
        }

        #endregion 生成统计表达式

        #endregion 统计

        #endregion 生成 SELECT

        #region 生成 FROM

        /// <summary>
        ///
        /// </summary>
        /// <param name="request">表别名。</param>
        private static void BuildSelectFrom(BuildSqlRequest request)
        {
            request.SqlBuilder.Append($" FROM `{GetTableName(request.Prdfix, request.Context)}` AS `{request.MasterAlias}`");

            //if (!string.IsNullOrWhiteSpace(context.Join))
            //{
            //    selectBuilder.Append(" ");
            //    selectBuilder.Append(context.Join);
            //}
        }

        #endregion 生成 FROM

        #region 生成排序

        private static void BuildOrderBy(BuildSqlRequest request)
        {
            var orderByFields = request.Context.OrderBy.ToList();

            if (!orderByFields.Any())
                return;

            var sql = string.Empty;

            foreach (var orderBy in orderByFields)
            {
                if (!string.IsNullOrWhiteSpace(sql))
                    sql += ", ";

                sql += GetField(request, orderBy.Name);
                sql += " ";
                sql += orderBy.Sort;
            }

            request.SqlBuilder.Append(" ORDER BY ");
            request.SqlBuilder.Append(sql);
        }

        #endregion 生成排序

        #endregion 生成查询语句

        #region 生成更新语句

        private static void BuildUpdate(BuildSqlRequest request)
        {
            request.SqlBuilder.Append($"UPDATE `{GetTableName(request.Prdfix, request.Context)}` AS `{request.MasterAlias}`");
            request.SqlBuilder.Append(" SET ");
        }

        #region 生成设置属性值

        private static void BuildUpdateSetProperty(BuildSqlRequest request)
        {
            var setBuilder = new StringBuilder();

            OnBuildUpdateUniqueProperty(request, setBuilder);
            OnBuildUpdateSetProperty(request, setBuilder);
            OnBuildUpdateAccumulatedProperties(request, setBuilder);

            request.SqlBuilder.Append(setBuilder);
        }

        private static void OnBuildUpdateUniqueProperty(BuildSqlRequest request, StringBuilder setBuilder)
        {
            if (!request.Context.SetUniqueProperty)
                return;

            var parameterName = $" `{request.MasterAlias}`.`{GetTableKey(request.Prdfix, request.Context)}`";

            setBuilder.Append($"{parameterName} = {parameterName}");
        }

        private static void OnBuildUpdateSetProperty(BuildSqlRequest request, StringBuilder setBuilder)
        {
            if (request.Context.SetProperties.Count <= 0)
                return;

            foreach (var property in request.Context.SetProperties)
            {
                if (setBuilder.Length > 0)
                {
                    setBuilder.Append(", ");
                }

                var parameterName = GetField(request, property.Key);

                var parameterValue = $"p_{request.Context.ParameterValues.Count.ToString().PadLeft(2, '0')}_SET";

                setBuilder.Append($"{parameterName} = @{parameterValue}");

                request.Context.ParameterValues[parameterValue] = property.Value;
            }
        }

        private static void OnBuildUpdateAccumulatedProperties(BuildSqlRequest request, StringBuilder setBuilder)
        {
            if (request.Context.AccumulatedProperties.Count <= 0)
                return;

            foreach (var property in request.Context.AccumulatedProperties)
            {
                if (setBuilder.Length > 0)
                {
                    setBuilder.Append(", ");
                }

                var parameterName = GetField(request, property.Key);

                var parameterValue = $"p_{request.Context.ParameterValues.Count.ToString().PadLeft(2, '0')}_SETADD";

                setBuilder.Append($"{parameterName} = {parameterName} + @{parameterValue}");

                request.Context.ParameterValues[parameterValue] = property.Value;
            }
        }

        #endregion 生成设置属性值

        #endregion 生成更新语句

        #region 生成删除语句

        private static void BuildDelete(BuildSqlRequest request)
        {
            request.SqlBuilder.Append($"DELETE `{request.MasterAlias}` FROM `{GetTableName(request.Prdfix, request.Context)}` AS `{request.MasterAlias}`");
        }

        #endregion 生成删除语句

        #region 生成 WHERE 条件

        private static void BuildWhere(BuildSqlRequest request)
        {
            if (request.Context.Where.Count <= 0)
                return;

            var whereBuilder = new StringBuilder();

            foreach (var where in request.Context.Where)
            {
                var sql = OnBuildWhere(request, where);

                if (string.IsNullOrWhiteSpace(sql))
                    continue;

                if (whereBuilder.Length > 0)
                {
                    whereBuilder.Append(" AND ");
                }

                whereBuilder.Append($"({sql})");
            }

            if (whereBuilder.Length > 0)
            {
                request.SqlBuilder.Append(" WHERE ");
                request.SqlBuilder.Append(whereBuilder.ToString());
            }
        }

        private static string OnBuildWhere(BuildSqlRequest request, WhereExpressionResult where)
        {
            var whereBuilder = new StringBuilder();

            if (where.ComparisonExpressionDescriptor != null)
            {
                var comparisonSQL = BuildComparisonSQL(request, where.ComparisonExpressionDescriptor);

                //whereBuilder.Append("(");
                whereBuilder.Append(comparisonSQL);
                //whereBuilder.Append(")");
            }

            if (where.LogicalExpressionDescriptor != null)
            {
                var logicalLeft = BuildLogicalSQL(request, where.LogicalExpressionDescriptor.Left);
                var logicalRight = BuildLogicalSQL(request, where.LogicalExpressionDescriptor.Right);

                //if (whereBuilder.Length > 0)
                //{
                //    whereBuilder.Append(" AND ");
                //}

                whereBuilder.Append("(");
                whereBuilder.Append(logicalLeft);
                whereBuilder.Append(")");
                whereBuilder.Append(" ");
                whereBuilder.Append(where.LogicalExpressionDescriptor.LogicalTag);
                whereBuilder.Append(" ");
                whereBuilder.Append("(");
                whereBuilder.Append(logicalRight);
                whereBuilder.Append(")");
            }

            if (where.MemberExpressionDescriptor != null)
            {
                //if (whereBuilder.Length > 0)
                //{
                //    whereBuilder.Append(" AND ");
                //}

                var sql = BuildMemberSQL(request, where.MemberExpressionDescriptor);

                //whereBuilder.Append("(");
                whereBuilder.Append(sql);
                //whereBuilder.Append(")");
            }

            if (where.CallExpressionDescriptor != null)
            {
                //if (whereBuilder.Length > 0)
                //{
                //    whereBuilder.Append(" AND ");
                //}

                var sql = BuildCallSQL(request, where.CallExpressionDescriptor);

                //whereBuilder.Append("(");
                whereBuilder.Append(sql);
                //whereBuilder.Append(")");
            }

            return whereBuilder.ToString();
        }

        #region 比较表达式

        private static string BuildComparisonSQL(BuildSqlRequest request, IWhereComparisonExpressionDescriptor where)
        {
            var left = where.Left;
            var right = where.Right;

            var leftSQL = string.Empty;
            var rightSQL = string.Empty;

            switch (left.ParmeterType)
            {
                case WhereComparisonExpressionParameterType.Parameter:
                    {
                        leftSQL += GetField(request, left.ParmeterValue.ToString());
                    }
                    break;

                case WhereComparisonExpressionParameterType.Value:
                    {
                        var parameterValue = $"p_{request.Context.ParameterValues.Count.ToString().PadLeft(2, '0')}";
                        leftSQL += $"@{parameterValue}";
                        request.Context.ParameterValues[parameterValue] = left.ParmeterValue;
                    }
                    break;
            }

            switch (right.ParmeterType)
            {
                case WhereComparisonExpressionParameterType.Parameter:
                    {
                        rightSQL += GetField(request, right.ParmeterValue.ToString());
                    }
                    break;

                case WhereComparisonExpressionParameterType.Value:
                    {
                        var parameterValue = $"p_{request.Context.ParameterValues.Count.ToString().PadLeft(2, '0')}";
                        rightSQL += $"@{parameterValue}";
                        request.Context.ParameterValues[parameterValue] = right.ParmeterValue;
                    }
                    break;
            }

            return leftSQL + " " + where.ComparisonDescriptor + " " + rightSQL;
        }

        #endregion 比较表达式

        #region 逻辑表达式

        private static string BuildLogicalSQL(BuildSqlRequest request, WhereLogicalExpressionParameter where)
        {
            var whereBuilder = new StringBuilder();

            if (where.ComparisonExpressionDescriptor != null)
            {
                var comparisonSQL = BuildComparisonSQL(request, where.ComparisonExpressionDescriptor);

                //whereBuilder.Append("(");
                whereBuilder.Append(comparisonSQL);
                //whereBuilder.Append(")");
            }

            if (where.LogicalExpressionDescriptor != null)
            {
                var logicalLeft = BuildLogicalSQL(request, where.LogicalExpressionDescriptor.Left);
                var logicalRight = BuildLogicalSQL(request, where.LogicalExpressionDescriptor.Right);

                //if (whereBuilder.Length > 0)
                //{
                //    whereBuilder.Append(" AND ");
                //}

                whereBuilder.Append("(");
                whereBuilder.Append(logicalLeft);
                whereBuilder.Append(")");
                whereBuilder.Append(" ");
                whereBuilder.Append(where.LogicalExpressionDescriptor.LogicalTag);
                whereBuilder.Append(" ");
                whereBuilder.Append("(");
                whereBuilder.Append(logicalRight);
                whereBuilder.Append(")");
            }

            if (where.MemberExpressionDescriptor != null)
            {
                //if (whereBuilder.Length > 0)
                //{
                //    whereBuilder.Append(" AND ");
                //}

                var sql = BuildMemberSQL(request, where.MemberExpressionDescriptor);

                //whereBuilder.Append("(");
                whereBuilder.Append(sql);
                //whereBuilder.Append(")");
            }

            if (where.CallExpressionDescriptor != null)
            {
                //if (whereBuilder.Length > 0)
                //{
                //    whereBuilder.Append(" AND ");
                //}

                var sql = BuildCallSQL(request, where.CallExpressionDescriptor);

                //whereBuilder.Append("(");
                whereBuilder.Append(sql);
                //whereBuilder.Append(")");
            }

            return whereBuilder.ToString();
        }

        #endregion 逻辑表达式

        #region 变量表达式

        private static string BuildMemberSQL(BuildSqlRequest request, IWhereMemberExpressionDescriptor where)
        {
            var whereBuilder = new StringBuilder();

            if (where.PropertyType == typeof(bool))
            {
                var parameterName = GetField(request, where.ParmeterName);

                whereBuilder.Append(parameterName);

                if (Convert.ToBoolean(where.ParmeterValue))
                {
                    whereBuilder.Append(" > 0");
                }
                else
                {
                    whereBuilder.Append(" <= 0");
                }
            }

            return whereBuilder.ToString();
        }

        #endregion 变量表达式

        #region 函数表达式

        private static string BuildCallSQL(BuildSqlRequest request, IWhereCallExpressionDescriptor where)
        {
            var whereBuilder = new StringBuilder();

            if (where.Like != null)
            {
                var parameterName = GetField(request, where.Like.ParameterName);
                var parameterValue = $"p_{request.Context.ParameterValues.Count.ToString().PadLeft(2, '0')}_like";
                var currentValue = where.Like.ParameterValue;

                switch (where.Like.Type)
                {
                    case WhereLikeCallExpressionParameterType.Equal:
                        whereBuilder.Append($"{parameterName} LIKE CONCAT('%',@{parameterValue},'%')");
                        request.Context.ParameterValues[parameterValue] = currentValue;
                        break;

                    case WhereLikeCallExpressionParameterType.StartsWith:
                        whereBuilder.Append($"{parameterName} LIKE CONCAT(@{parameterValue},'%')");
                        request.Context.ParameterValues[parameterValue] = currentValue;
                        break;

                    case WhereLikeCallExpressionParameterType.EndsWith:
                        whereBuilder.Append($"{parameterName} LIKE CONCAT('%',@{parameterValue})");
                        request.Context.ParameterValues[parameterValue] = currentValue;
                        break;
                }
            }

            if (where.Between != null)
            {
                var startValue = $"p_{request.Context.ParameterValues.Count.ToString().PadLeft(2, '0')}_between";
                request.Context.ParameterValues[startValue] = where.Between.StartValue;

                var endValue = $"p_{request.Context.ParameterValues.Count.ToString().PadLeft(2, '0')}_between";
                request.Context.ParameterValues[endValue] = where.Between.EndValue;

                whereBuilder.Append(GetField(request, where.Between.ParameterName));
                whereBuilder.Append(" BETWEEN ");

                whereBuilder.Append($"@{startValue}");
                whereBuilder.Append(" AND ");
                whereBuilder.Append($"@{endValue}");
            }

            if (where.In != null)
            {
                if (where.In.ParameterValue.Count > 0)
                {
                    whereBuilder.Append(GetField(request, where.In.ParameterName));
                    whereBuilder.Append(" IN (");

                    var index = 0;
                    foreach (var parameterValue in where.In.ParameterValue)
                    {
                        if (index > 0)
                        {
                            whereBuilder.Append(", ");
                        }

                        var pValue = $"p_{request.Context.ParameterValues.Count.ToString().PadLeft(2, '0')}_in";
                        request.Context.ParameterValues[pValue] = parameterValue;

                        whereBuilder.Append($"@{pValue}");
                        index++;
                    }

                    whereBuilder.Append(")");
                }
            }

            if (where.NotIn != null)
            {
                if (where.NotIn.ParameterValue.Count > 0)
                {
                    whereBuilder.Append(GetField(request, where.NotIn.ParameterName));
                    whereBuilder.Append(" NOT IN (");

                    var index = 0;
                    foreach (var parameterValue in where.NotIn.ParameterValue)
                    {
                        if (index > 0)
                        {
                            whereBuilder.Append(", ");
                        }

                        var pValue = $"p_{request.Context.ParameterValues.Count.ToString().PadLeft(2, '0')}_in";
                        request.Context.ParameterValues[pValue] = parameterValue;

                        whereBuilder.Append($"@{pValue}");
                        index++;
                    }

                    whereBuilder.Append(")");
                }
            }

            return whereBuilder.ToString();
        }

        #endregion 函数表达式

        /// <summary>
        /// 生成 SQL 字段名。
        /// </summary>
        /// <param name="request"></param>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        private static string GetField(BuildSqlRequest request, string parameterName)
        {
            if (request.Context.Fields == null || !request.Context.Fields.Any())
            {
                return $"`{request.MasterAlias}`.`{parameterName}`";
            }

            var parameter = request.Context.Fields.Where(item => item.Name == parameterName).FirstOrDefault();

            if (parameter == null)
            {
                return $"`{request.MasterAlias}`.`{parameterName}`";
            }

            var fieldValue = string.Empty;

            if (!parameter.Alias.IsNullOrWhiteSpace() && parameter.Alias != parameter.Name)
            {
                fieldValue = $"`{request.MasterAlias}`.`{parameter.Name}` AS `{parameter.Alias}`";
            }
            else
            {
                fieldValue = $"`{request.MasterAlias}`.`{parameter.Name}`";
            }

            return fieldValue;
        }

        #endregion 生成 WHERE 条件

        #region 生成 LIMIT

        private static void BuildLimit(BuildSqlRequest request)
        {
            var context = request.Context;

            var limit = string.Empty;

            if (context.Take > 0 || context.Skip > 0)
            {
                if (context.Skip <= 0)
                {
                    context.Skip = 0;
                }

                if (context.Take <= 0)
                {
                    context.Take = 100;
                }

                limit = $"{context.Skip}, {context.Take}";
            }

            if (limit.Length > 0)
            {
                request.SqlBuilder.Append($" LIMIT {limit}");
            }
        }

        #endregion 生成 LIMIT

        #region 生成主键名

        /// <summary>
        /// 生成主键名。
        /// </summary>
        /// <param name="prdfix">表前缀。</param>
        /// <param name="context"></param>
        /// <returns></returns>
        private static string GetTableKey(string prdfix, ExpressionContext context)
        {
            return DataModelManager.Instance.GetTableKey(prdfix, context.EntityType);
        }

        #endregion 生成主键名

        #region 生成表名

        /// <summary>
        /// 生成表名。
        /// </summary>
        /// <param name="prdfix">表前缀。</param>
        /// <param name="context"></param>
        /// <returns></returns>
        private static string GetTableName(string prdfix, ExpressionContext context)
        {
            return DataModelManager.Instance.GetTableName(prdfix, context.EntityType);
        }

        #endregion 生成表名
    }
}